package com.swss.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class StringUtil {
	public String translate(String str, String oldCode, String newCode) {
		String tempStr = "";
		try {
			tempStr = new String(str.getBytes(oldCode), newCode);
			tempStr = tempStr.trim();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		return tempStr;
	}

	public static void changeEncoding(String inEncoding, String outEncoding,
			String inFileName, String outFileName) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(inFileName), inEncoding));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(outFileName), outEncoding));
		String s = null;
		while ((s = reader.readLine()) != null) {
			writer.write(s, 0, s.length());
			writer.newLine();
		}
		writer.flush();
		writer.close();
		reader.close();
	}

	public void writeTest(){
		try {
			FileOutputStream fos = new FileOutputStream("D:\\npm2.log");
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			BufferedWriter bw = new BufferedWriter(osw);
			
			bw.write("aaaaa");
			bw.newLine();
			bw.write("aaaaa");
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void readTest(){
		try {
			FileInputStream fis = new FileInputStream("D:\\npm-debug.log");
			InputStreamReader isr=new InputStreamReader(fis,"gbk");
			BufferedReader br = new BufferedReader(isr);
			String s="";
			
			FileOutputStream fos = new FileOutputStream("D:\\npm2.log");
			OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
			BufferedWriter bw = new BufferedWriter(osw);
			
			while((s=br.readLine())!=null){
				System.out.println(s);
				bw.write(s, 0, s.length());
				bw.newLine();
			}
			bw.close();
			br.close();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {

		new StringUtil().readTest();
	
		
		System.out.println(new StringUtil()
				.translate("头部菜单", "utf-8", "gb2312"));

		// try {
		// changeEncoding("GBK", "UTF-8", "gbk.txt", "utf8.txt");
		// } catch (IOException e) {
		// System.out.println("转换失败，原因：" + e.getMessage());
		// }

	}

}
